package com.person.mall.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.person.mall.dao.ShippingMapper;
import com.person.mall.enums.ResponseEnum;
import com.person.mall.form.ShippingForm;
import com.person.mall.pojo.Shipping;
import com.person.mall.service.IShippingService;
import com.person.mall.vo.ResponseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.person.mall.enums.ResponseEnum.DELETE_SHIPPING_SUCCESS;
import static com.person.mall.enums.ResponseEnum.UPDATE_SHIPPING_ERROR;

/**
 * @author 最酷的桃儿wjc
 * * @date 2022/5/23
 */
@Service
public class ShippingService implements IShippingService {


    @Autowired
    private ShippingMapper shippingMapper;
    /**
     * 添加收货地址
     * @param shippingForm
     * @return
     */

    @Override
    public ResponseVo<Map<String,Integer>> addShipping(Integer uid, ShippingForm shippingForm) {
        Shipping shipping = new Shipping();
        BeanUtils.copyProperties(shippingForm,shipping);
        shipping.setUserId(uid);
        int addFlag = shippingMapper.insert(shipping);
        if (addFlag == 0){
            return ResponseVo.error(UPDATE_SHIPPING_ERROR);
        }
        Map<String,Integer> shippingMap = new HashMap<>();
        shippingMap.put("shippingId",shipping.getId());
        return ResponseVo.successGetData(shippingMap);
    }

    /**
     * 根据用户id、收货地址id删除收货地址
     * @param uid
     * @param shippingId
     * @return
     */
    @Override
    public ResponseVo delShipping(Integer uid, Integer shippingId) {
        int delFlag = shippingMapper.deleteByIdAndUid(uid, shippingId);
        if (delFlag == 0){
            return ResponseVo.error(DELETE_SHIPPING_SUCCESS);
        }
        return ResponseVo.success(DELETE_SHIPPING_SUCCESS);
    }

    /**
     * 更新购物车信息
     * @param uid
     * @param shippingId
     * @param shippingForm
     * @return
     */
    @Override
    public ResponseVo updateShipping(Integer uid, Integer shippingId, ShippingForm shippingForm) {

        Shipping shipping = new Shipping();
        BeanUtils.copyProperties(shippingForm,shipping);
        shipping.setId(shippingId);
        shipping.setUserId(uid);
        int updateFlag = shippingMapper.updateByPrimaryKeySelective(shipping);
        if (updateFlag == 0){
            ResponseVo.error(UPDATE_SHIPPING_ERROR);
        }
        return ResponseVo.error(UPDATE_SHIPPING_ERROR);
    }


    /**
     * 根据uid获取地址列表
     * @param uid
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ResponseVo<PageInfo> shippingList(Integer uid, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum,pageSize);

        List<Shipping> shippingList = shippingMapper.selectByUid(uid);

        PageInfo pageInfo = new PageInfo(shippingList);
        return ResponseVo.successGetData(pageInfo);
    }
}
